package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net"
	"os"
)

func main() {

	r := gin.Default()
	r.GET("/", func(c *gin.Context) {

		serverIP, err := getServerIP()
		if err != nil {
			c.JSON(500, gin.H{"error": err.Error()})
			return
		}
		aversion := os.Getenv("APP_VERSION")
		if aversion == "" {
			aversion = "当前镜像版本为latest"
		}
		c.JSON(200, gin.H{"server_ip": serverIP,

			"APP_VERSION": aversion})
	})
	r.Run(":3003")
}
func getServerIP() (string, error) {
	addrs, err := net.InterfaceAddrs()
	if err != nil {
		return "", err
	}

	for _, addr := range addrs {
		if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() && ipnet.IP.To4() != nil {
			return ipnet.IP.String(), nil
		}
	}

	return "", fmt.Errorf("no valid IP address found")
}
